<template>
	<view class="container">
		<u-toast ref="uToast" />
		<u-select v-model="show" :list="list" @confirm="confirm"></u-select>
		<u-select v-model="condition" mode="mutil-column-auto" :list="teacher_list" @confirm="ensure"></u-select>
		<u-select v-model="boolen" mode="mutil-column-auto" :list="address_list" @confirm="submit"></u-select>
		<u-navbar :is-back="true" title="编辑设备" back-icon-color='#333333' height="40" :border-bottom="false">
		</u-navbar>
		<u-mask :show="display">
			<view class="warp">
				<view class="rect" @tap.stop>
					<view class="tanchu">
						确认删除该设备?
					</view>
					<view class="anniu">
						<view class="" @click="display=false">
							取消
						</view>
						<view class="" @click="deleteGoods">
							确定
						</view>
					</view>
				</view>
			</view>
		</u-mask>
		<view class="list">
			<view class="description">
				<text>设备名称</text>
				<text>{{name}}</text>
			</view>
			<view class="type">
				<text>设备类型</text>
				<text>{{type}}</text>
				<text @click="show=true"></text>
			</view>
			<view class="address">
				<text>存放场地</text>
				<text>{{rooms}}</text>
				<text @click="boolen=true">选择场地</text>
			</view>
			<view class="principal">
				<text>配属人员</text>
				<text>{{teacher_name}}</text>
				<text @click="condition=true">选择人员</text>
			</view>
		</view>
		<view class="entering">
			<text>录入人员</text>
			<text>{{staff}}</text>
		</view>
		<view class="confirm">
			<view class="cancel" @click="display=true">
				{{cancel}}
			</view>
			<view class="verify" @click="updateGoods">
				{{verify}}
			</view>
		</view>
	</view>
</template>

<script>
	import request from '../../request.js'
	export default {
		data() {
			return {
				boolen: false,
				cancel: '删除',
				name: '计算机',
				goods_id: '',
				type: '信息化',
				teacher_name: '',
				rooms: '校长室',
				staff: '',
				condition: false,
				boolen: false,
				show: false,
				display: false,
				verify: '确认',
				list: [{
						value: '1',
						label: '信息化'
					},
					{
						value: '2',
						label: '总务'
					},
					{
						value: '3',
						label: '教学'
					},
					{
						value: '4',
						label: '其他'
					},
				],
				address_list: [],
				teacher_list: [],
				form: {
					goods_id: '',
					name: '',
					goods_type: '',
					teacher_user_ids: '',
					room_ids: ''
				},
				teacher: '',
				address_arr: [],
				people_arr: [],
			}
		},
		onLoad(option) {
			if (option) {
				this.name = option.name;
				this.type = option.type;
				this.rooms = option.rooms;
				this.goods_id = option.goods_id;
				this.teacher_name = option.teacher_users;
				this.staff = option.full_name;
				this.form.goods_type = option.type;
				if(option.room_list){
					this.form.room_ids = option.room_list;
				}
				if(option.teacher_list){
					    this.form.teacher_user_ids = option.teacher_list;
				}
			}
			this.buildingLists();
			this.groupLists();
			this.form.name = this.name;
			this.form.goods_id = this.goods_id;
		},
		methods: {
			changeBoolen() {
				this.boolen = !this.boolen;
			},
			confirm(e) {
				console.log(e);
				this.type = e[0].label;
				this.form.goods_type = e[0].label;
			},
			ensure(e) {
				console.log(e[1].label);
				this.teacher_name = e[1].label;
				this.people_arr.push(e[1].value);
				if (this.people_arr.length > 1) {
					this.form.teacher_user_ids = this.people_arr.split(',');
				} else {
					this.form.teacher_user_ids = this.people_arr[0];
				}
			},
			submit(e) {
				console.log(e);
				this.rooms = `${e[0].label}${e[1].label}${e[2].label}`;
				console.log(e[2].value);
				this.address_arr.push(e[2].value);
				if (this.address_arr.length > 1) {
					this.form.room_ids = this.address_arr.split(',');
				} else {
					this.form.room_ids = this.address_arr[0];
				}


			},
			buildingLists() {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'teacher/general/buildingLists',
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data.data);
							res.data.data.forEach((item, index) => {
								_this.address_list.push({
									label: item.name,
									value: item.building_id,
									children: [],
								})
								_this.floorLists(item.building_id, index);

							})
							console.log(_this.address_list);

						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			floorLists(param, param2) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let building_id = param;
				let indexes = param2;
				let _this = this;
				request({
					url: base_url + 'teacher/general/floorLists',
					method: 'post',
					data: {
						building_id: building_id
					},
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data.data);
							res.data.data.forEach((item, index) => {
								_this.address_list[param2].children.push({
									label: item.name,
									value: item.floor_id,
									children: [],
								})
								_this.roomLists(building_id, indexes, item.floor_id, index);
							})
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			roomLists(param1, param2, param3, param4) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'teacher/general/roomLists',
					method: 'post',
					data: {
						building_id: param1,
						floor_id: param3
					},
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data.data);
							res.data.data.forEach(item => {
								if (_this.address_list[param2].children[param4]) {
									_this.address_list[param2].children[param4].children.push({
										label: item.name,
										value: item.room_id,
									})
								}
							})
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			groupLists() {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'teacher/general/groupLists',
					data: {
						school_district_id: uni.getStorageSync('school_district_id')
					},
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data.data);
							res.data.data.forEach((item, index) => {
								_this.teacher_list.push({
									label: item.name,
									value: item.subject_id,
									children: []
								})
								_this.userLists(item.subject_id, index);
							})

						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			userLists(param1, param2) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'teacher/general/groupUserLists',
					data: {
						subject_id: param1
					},
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data.data.data);
							res.data.data.data.forEach(item => {
								_this.teacher_list[param2].children.push({
									label: item.teacher_info ? item.teacher_info.full_name : '',
									value: item.teacher_user_id,
								})
							})

						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			updateGoods() {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'teacher/goods/updateGoods',
					data: {
						...this.form,
					},
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data);
							_this.$toLink('../inventory_management/inventory_management', {}, 2);
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			deleteGoods() {
				this.display = false;
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'teacher/goods/deleteGoods',
					data: {
						goods_id: this.goods_id
					},
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							console.log(res.data);
							_this.$toLink('../inventory_management/inventory_management', {}, 2);
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			}
		}

	}
</script>

<style lang="scss">
	.container {
		width: 100%;
		height: 100vh;
		background-color: #F2F2F2;

		.warp {
			display: flex;
			align-items: flex-start;
			justify-content: center;
			height: 100%;

			.rect {
				margin-top: 450rpx;
				border-radius: 10rpx;
				width: 480rpx;
				height: 250rpx;
				background-color: #fff;

				.tanchu {
					width: 100%;
					height: 150rpx;
					font-size: 33rpx;
					color: #3E3E3E;
					text-align: center;
					line-height: 150rpx;
				}

				.anniu {
					width: 100%;
					height: 100rpx;
					display: flex;
					justify-content: space-around;
					align-items: center;

					view {
						border-radius: 30rpx;
						width: 150rpx;
						height: 60rpx;
						text-align: center;
						line-height: 60rpx;
						font-size: 26rpx;

						&:nth-child(1) {
							color: #2C78FE;
							margin-left: 15rpx;
							border: 1px solid #2C78FE;
							background: #fff;

						}

						&:nth-child(2) {
							color: #fff;
							margin-right: 15rpx;
							background: linear-gradient(to right, #2C78FE, #32B0FF);
						}


					}
				}
			}
		}

		.list {
			background-color: #fff;
			margin-top: 20rpx;
			width: 710rpx;
			margin-left: 20rpx;
			border-radius: 12rpx;

			.description {
				width: 94%;
				margin-left: 3%;
				height: 90rpx;
				border-bottom: 1px solid #F2F2F2;
				display: flex;
				justify-content: flex-start;
				align-items: center;

				text {
					&:nth-child(1) {
						color: #666666;
						font-size: 26rpx;
						width: 150rpx;
					}

					&:nth-child(2) {
						color: #333333;
						font-size: 26rpx;
						width: 500rpx;
						text-align: right;
					}
				}

			}

			.type {
				width: 94%;
				margin-left: 3%;
				height: 90rpx;
				border-bottom: 1px solid #F2F2F2;
				display: flex;
				justify-content: flex-start;
				align-items: center;

				text {
					font-size: 26rpx;

					&:nth-child(1) {
						color: #666666;
					}

					&:nth-child(2) {
						width: 420rpx;
						color: #333333;
						margin-left: 80rpx;
						text-align: right;
					}

					&:nth-child(3) {
						width: 0rpx;
						margin-left: 20rpx;
						margin-top: 20rpx;
						height: 0rpx;
						border-width: 18rpx 18rpx 18rpx 18rpx;
						border-color: #ccc transparent transparent transparent;
						border-style: solid;
					}
				}
			}

			.address {
				width: 94%;
				margin-left: 3%;
				height: 90rpx;
				border-bottom: 1px solid #F2F2F2;
				display: flex;
				justify-content: flex-start;
				align-items: center;

				text {
					&:nth-child(1) {
						color: #666666;
						font-size: 26rpx;
						width: 150rpx;

					}

					&:nth-child(2) {
						font-size: 26rpx;
						text-align: right;
						color: #333333 !important;
						width: 300rpx;
						margin-left: 80rpx;

					}

					&:nth-child(3) {
						width: 107rpx;
						height: 40rpx;
						text-align: center;
						line-height: 40rpx;
						font-size: 20rpx;
						color: #333333;
						background-color: #EDF4FB;
						border: 1rpx solid #ccc;
						border-radius: 5rpx;
						margin-left: 20rpx;
					}
				}
			}

			.principal {
				width: 94%;
				margin-left: 3%;
				height: 90rpx;
				border-bottom: 1px solid #F2F2F2;
				display: flex;
				justify-content: flex-start;
				align-items: center;

				text {
					&:nth-child(1) {
						color: #666666;
						font-size: 26rpx;
						width: 150rpx;

					}

					&:nth-child(2) {
						font-size: 26rpx;
						color: #333333 !important;
						width: 300rpx;
						margin-left: 80rpx;
						text-align: right;

					}

					&:nth-child(3) {
						width: 107rpx;
						height: 40rpx;
						text-align: center;
						line-height: 40rpx;
						font-size: 20rpx;
						color: #333333;
						background-color: #EDF4FB;
						border: 1rpx solid #ccc;
						border-radius: 5rpx;
						margin-left: 20rpx;
					}
				}
			}
		}

		.entering {
			background-color: #fff;
			margin-top: 20rpx;
			width: 710rpx;
			height: 90rpx;
			margin-left: 20rpx;
			border-radius: 12rpx;
			display: flex;
			align-items: center;
			justify-content: flex-start;

			text {
				font-size: 26rpx;

				&:nth-child(1) {
					color: #666666;
					width: 140rpx;
					margin-left: 25rpx;
				}

				&:nth-child(2) {
					margin-left: 10rpx;
					color: #333333;
					text-align: right;
					width: 500rpx;

				}
			}
		}

		.confirm {
			width: 100%;
			height: 400rpx;
			display: flex;
			justify-content: space-around;
			align-items: center;

			.verify {
				width: 200rpx;
				height: 60rpx;
				border-radius: 50rpx;
				text-align: center;
				line-height: 60rpx;
				background: linear-gradient(to right, #2C78FE, #32B0FF);
				color: #fff;
				font-size: 30rpx;
			}

			.cancel {
				width: 200rpx;
				height: 60rpx;
				border-radius: 50rpx;
				text-align: center;
				line-height: 60rpx;
				background: #fff;
				color: #666666;
				font-size: 30rpx;
			}
		}
	}
</style>
